class MessageItem{
	constructor(option){
		this.eventName=option.eventName==undefined?null:option.eventName;
		this.id=function(){
			if(option.eventName==undefined){
				return option.watchEventName+'_'+new Date().getTime();
			}else{
				return option.eventName+'_'+new Date().getTime();
			}; 
		}();
		this.runId=option.runId,
		this.messageType=null;
		this.data=option.data;
		this.results=null;
		this.index=0;
		this.watchEventName=option.watchEventName;
		this.watchEventCallback=option.watchEventCallback;
		this.isRepeatSend=option.isRepeatSend==undefined?false:option.isRepeatSend;
		this.timer=null;
		this.timerFtn=option.timerFtn;
		this.init();
	}
	init(){
		if(this.eventName != null){
			this.messageType='once';
			this.timerFtn(this.createSendData());
			if(this.isRepeatSend) this.setInterval();
		}else{
			this.messageType='watch';
		};
	}
	createSendData(){
		var tempObj =new Object();
		for(var key in this.data){
			tempObj[key]=this.data[key];
		};
		tempObj['runId']=this.runId;
		return {
			smid:this.id,
			data:tempObj
		};
	}
	setInterval(){
		var _self =this;
		this.timer=setInterval(()=>{
			if(_self.index<3){
				this.timerFtn(this.createSendData());
				_self.index++;
			}else{
				_self.results='error';
				this.watchEventCallback({
					results:_self.results,
					data:null
				});
				clearInterval(_self.timer);
			};
		},3000);
	}
	returnSuccessCallback(){
		if(this.messageType == 'once'){
			return this._returnOnceWatchCallback();
		}else if(this.messageType == 'watch'){
			return this._returnKeepWatchCallback();
		};
	}
	_returnKeepWatchCallback(){
		var _self =this
		return function(data){
			_self.results='success';
			_self.watchEventCallback({
				results:_self.results,
				data:data			
			});
		};
	}
	_returnOnceWatchCallback(){ 
		var _self =this
		return function(data){
			clearInterval(_self.timer);
			_self.results='success';
			_self.watchEventCallback({
				results:_self.results,
				data:data				
			});
		};
	}
}
export default MessageItem